//
//  KKReplyVC.m
//  DoMi
//
//  Created by 康凯 on 2016/11/29.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKReplyVC.h"
#import "KKCommentCell.h"
#import "KKStudyHttp.h"
#import "KKPersonalHomeVC.h"
#import "IQKeyboardManager.h"
#import "KKMyHttp.h"

@interface KKReplyVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *commentAvatar;
@property (weak, nonatomic) IBOutlet UILabel *commentName;
@property (weak, nonatomic) IBOutlet UILabel *comment;
@property (weak, nonatomic) IBOutlet UILabel *replayCount;
@property (weak, nonatomic) IBOutlet UILabel *likeCount;
@property (weak, nonatomic) IBOutlet UILabel *commentTime;
@property (weak, nonatomic) IBOutlet UILabel *job;
@property (weak, nonatomic) IBOutlet UILabel *label;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(strong,nonatomic)NSArray *replyDatas;

@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (weak, nonatomic) IBOutlet UITextField *input;
@property (weak, nonatomic) IBOutlet UIView *replyView;
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentHeight;

@end

@implementation KKReplyVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar=NO;
    [self setNavBlack];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self unRegisterForKeyboardNotifications];
    [IQKeyboardManager sharedManager].enableAutoToolbar=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"评论回复";
    [self registerForKeyboardNotifications];
    [self initData];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKCommentCell" bundle:nil] forCellReuseIdentifier:@"KKCommentCell"];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeClick)];
    [self.replyView addGestureRecognizer:tap];
    [self getReplyList];
}
-(void)initData{
    [self.commentAvatar sd_setImageWithURL:[NSURL URLWithString:self.commentInfo.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAvatar)];
    [self.commentAvatar addGestureRecognizer:tap1];
    self.commentName.text=self.commentInfo.nickname;
    self.job.text=self.commentInfo.job;
    self.label.text=self.commentInfo.industry;
    CGFloat tmp=80+[KKAppUtils sizeWithText:self.commentInfo.commentcontent font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(screenWidth-61, MAXFLOAT)].height;
    if(tmp>130){
        self.commentHeight.constant=130;
    }else{
        self.commentHeight.constant=tmp;
    }
    self.comment.text=self.commentInfo.commentcontent;
    self.replayCount.text=self.commentInfo.reply;
    self.likeCount.text=self.commentInfo.commentlike;
    self.commentTime.text=self.commentInfo.commenttime;
    
}
#pragma mark 按钮--头像
-(void)pushAvatar{
    KKPersonalHomeVC *personHome=[[KKPersonalHomeVC alloc] init];
    personHome.expertId=self.commentInfo.uid;
    [self.navigationController pushViewController:personHome animated:YES];
}
#pragma mark 按钮--点赞
-(void)likeClick{
    [KKStudyHttp addCommentLike:self.commentInfo.commentid success:^(id data) {
        self.likeCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 按钮--隐藏键盘
- (IBAction)keyboardHiddenClick:(id)sender {
    [self.view endEditing:YES];
}
#pragma mark 按钮--发送回复
- (IBAction)commentClick:(id)sender {
    [KKMyHttp userCheck:^(id data) {
        if([KKAppUtils isEmptyString:self.input.text]){
            [self toast:@"请输入回复内容"];
            return;
        }
        [self addCommentReply];
    } failure:^(NSError *error) {
        
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKCommentCell *comment=(KKCommentCell *)cell;
    [comment initReplyData:self.replyDatas[indexPath.row]];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.replyDatas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKCommentCell"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    KKCommentM *comment=self.replyDatas[indexPath.row];
    return 100+[KKAppUtils sizeWithText:comment.commentcontent font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(screenWidth-61, MAXFLOAT)].height;
}
#pragma mark 键盘相关
//实现当键盘出现的时候计算键盘的高度大小
- (void)keyboardShow:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    //获取键盘高度
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    [UIView animateWithDuration:0 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        self.inputView.transform=CGAffineTransformMakeTranslation(0, -kbSize.height);
    } completion:nil];
}

//当键盘隐藏的时候
- (void)keyboardHidden
{
    [UIView animateWithDuration:0 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        self.inputView.transform=CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        self.input.text=@"";
    }];
}

- (void)registerForKeyboardNotifications
{
    //监听键盘出现
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
    //监听键盘隐藏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden) name:UIKeyboardWillHideNotification object:nil];
    
}
- (void)unRegisterForKeyboardNotifications
{
    //释放
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

#pragma mark 接口--回复列表
-(void)getReplyList{
    [self showDialog];
    [KKStudyHttp getReplyList:self.commentInfo.commentid success:^(NSArray *datas) {
        self.replyDatas=datas;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 接口--评论回复
-(void)addCommentReply{
    [self showDialog];
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"commentid":self.commentInfo.commentid,@"replycontent":self.input.text};
    [KKStudyHttp addVideoReply:params success:^(id data) {
        [self dismissDialog];
        [self toast:@"回复成功"];
        [self.view endEditing:YES];
        [self getReplyList];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
@end
